<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check="false"
    >
      <!-- <van-cell v-for="(item, index) in list" :key="index" :title="item.content" /> -->
      <comment-item @reply="onReply" :comment="item" v-for="(item, index) in list" :key="index" />
    </van-list>
  </div>
</template>

<script>
import { getComment } from '@/api/comment.js'
import commentItem from '@/components/comments/commentItem.vue'
import { eventBus } from '@/api/eventBus.js'
export default {
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },
  props: {
    type: {
      type: String,
      requried: true
    },
    source: {
      type: [Number, String, Object],
      requried: true
    },
    list: {
      type: Array,
      requery: true
    }
  },
  components: {
    commentItem
  },
  created () {
    this.onLoad()
  },
  methods: {
    async onLoad () {
      try {
        const { data: res } = await getComment({
          type: this.type,
          source: this.source + '',
          offset: this.offset,
          limit: this.limit
        })
        this.list.push(...res.data.results)
        this.$emit('sendTotal', res.data.total_count)
        // 结束当前加载状态
        this.loading = false
        // 判断数据是否加载完成
        if (res.data.results.length > 0) {
          this.offset = res.data.last_id
        } else {
          this.finished = true
        }
      } catch (err) {
        console.log(err)
      }
    },
    onReply (e) {
      eventBus.$emit('reply', e)
    }
  }
}
</script>

<style scoped>

</style>
